<template>
  <div class="padding-middle">
    <el-card>
      <h3 style="text-align: center">上传课程</h3>
      <el-form ref="ruleForm" :model="postForm" :rules="rules" style="width: 1000px" label-width="80px" label-position="right" class="demo-ruleForm" auto-complete="off">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label="考试类型" prop="examType" auto-complete="off">
                <el-select v-model="postForm.examType" placeholder="请选择考试类型" style="width: 220px" @change="changekaoshi">
                  <el-option
                    v-for="(item,index) in options1"
                    :key="index"
                    :label="item"
                    :value="item"/>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label="课程分类" prop="courseCategoryId" auto-complete="off">
                <el-select v-model="postForm.courseCategoryId" placeholder="请选择课程分类" style="width: 220px" @change="changefenlei">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课程科目" prop="courseSubjectId" auto-complete="off">
              <el-select v-model="postForm.courseSubjectId" placeholder="请选择课程科目" style="width: 220px" >
                <el-option
                  v-for="item in options3"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label="课程名称" prop="name">
                <el-input v-model="postForm.name" placeholder="请输入课程名称" style="width: 220px"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label="课程时长" prop="courseTime">
                <el-input v-model="postForm.courseTime" placeholder="请输入课程时长" style="width: 220px"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课后练习" prop="homework" auto-complete="off">
              <el-select v-model="postForm.homework" placeholder="请选择课后练习" style="width: 220px" @change="changeCoures">
                <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="主讲人" prop="speaker">
          <el-input v-model="postForm.speaker" placeholder="请输入主讲人" style="width: 220px"/>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="视频上传" prop="url">
              <!-- action必选参数, 上传的地址 -->
              <el-upload
                :show-file-list="false"
                :on-success="handleVideoSuccess"
                :before-upload="beforeUploadVideo"
                :on-progress="uploadVideoProcess"
                :action="version + '/uploadFile'"
                style="width: 200px"
                class="avatar-uploader el-upload--text">
                <video v-if="postForm.url !='' && videoFlag == false" :src="version + '/uploadFile/path/'+postForm.url" class="avatar" controls="controls">您的浏览器不支持视频播放</video>
                <i v-else-if="postForm.url =='' && videoFlag == false" class="el-icon-plus avatar-uploader-icon"/>
                <el-progress
                  v-if="videoFlag == true"
                  :percentage="videoUploadPercent"
                  type="circle"
                  style="margin-top:30px;"/>
              </el-upload>
              <P class="text">请保证视频格式正确，请上传MP4格式</P>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>

<script>
import ElFormItem from '../../../../../node_modules/element-ui/packages/form/src/form-item.vue'
import { VERSION } from '@/constants/uri'
import { types } from '@/assets/type.js'
import axios from 'axios'
export default {
  components: { ElFormItem },
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.postForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      imageUrl: '',
      videoFlag: false,
      version: VERSION,
      item: null,
      videoUploadPercent: 0,
      params: {
        size: 10,
        examType: 'all',
        courseCategoryId: 'all',
        courseSubjectId: 'all'
      },
      postForm: {
        name: '',
        courseCategoryId: '',
        courseSubjectId: '',
        examType: '',
        courseTime: '',
        homework: '',
        speaker: '',
        courseCover: null,
        url: ''
      },
      rules: {
        examType: [
          { required: true, message: '请选择考试类型', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入课程名称', trigger: 'blur' }
        ],
        courseCategoryId: [
          { required: true, message: '请上选择课程分类', trigger: 'change' }
        ],
        courseSubjectId: [
          { required: true, message: '请上选择课程科目', trigger: 'change' }
        ],
        courseTime: [
          { required: true, message: '请输入课程时长', trigger: 'blur' }
        ],
        homework: [
          { required: true, message: '请选择课后练习', trigger: 'blur' }
        ],
        speaker: [
          { required: true, message: '请输入主讲人', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请上传课程视频', trigger: 'change' }
        ]
      },
      options2: [{
        value: '无',
        label: '无'
      }, {
        value: '正在学习',
        label: '正在学习'
      }, {
        value: '未学习',
        label: '未学习'
      }],
      options1: types,
      options3: [],
      options: []
    }
  },
  mounted() {
    if (this.$route.params.index && this.$route.params.page) {
      this.params.page = this.$route.params.page * 1
      this.queryList(this.$route.params.index * 1)
    }
  },
  methods: {
    queryList(index) {
      axios.post(`/getCourseInfoList`, this.params).then(res => {
        if (res.success) {
          this.item = res.data[index]
          this.total = res.total
          this.getCourseCategoryListXiaLa(this.item.examType)
          this.getCourseSubjectListXiaLa(this.item.courseCategoryId)
          this.postForm.examType = this.item.examType
          this.postForm.courseCategoryId = this.item.courseCategoryId
          this.postForm.courseSubjectId = this.item.courseSubjectId
          this.postForm.name = this.item.name
          this.postForm.courseTime = this.item.courseTime
          this.postForm.homework = this.item.homework
          this.postForm.speaker = this.item.speaker
          this.postForm.courseCover = this.item.courseCover
          this.postForm.url = this.item.url
          this.postForm.id = this.item.id
        }
      })
    },
    changeCoures(option) {
    },
    uploadSuccessTitle(res) {
      this.postForm.courseCover = res.fileUploadPath
    },
    cancel() {
      this.$router.push('/coursemanagement/uploadCourse/list')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post(`/saveCourseInfo`, this.postForm).then(res => {
            if (res.success) {
              this.$router.push('/coursemanagement/uploadCourse/list')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changekaoshi(option) {
      this.postForm.courseCategoryId = ''
      this.getCourseCategoryListXiaLa(option)
    },
    changefenlei(option) {
      this.postForm.courseSubjectId = ''
      this.getCourseSubjectListXiaLa(option)
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    getCourseCategoryListXiaLa(option) {
      axios.get(`/getCourseCategoryListXiaLa/${option}`).then(res => {
        if (res.success) {
          const items = []
          res.data.forEach((item) => {
            items.push({ value: item.id, label: item.name })
          })
          this.options = items
        }
      })
    },
    getCourseSubjectListXiaLa(option) {
      axios.get(`/getCourseSubjectListXiaLa/${option}`).then(res => {
        if (res.success) {
          const items = []
          res.data.forEach((item) => {
            items.push({ value: item.id, label: item.name })
          })
          this.options3 = items
        }
      })
    },
    beforeUploadVideo(file) {
    },
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true
      this.videoUploadPercent = file.percentage.toFixed(0) * 1
    },
    handleVideoSuccess(res, file) {
      this.videoFlag = false
      this.videoUploadPercent = 0
      if (res.status === 200) {
        this.postForm.url = res.fileUploadPath
      } else {
        this.$message.error('视频上传失败，请重新上传！')
      }
    }
  }
}
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
